//
//  WBHomeViewController.swift
//  新浪微博
//
//  Created by ZJ on 2017/10/10.
//  Copyright © 2017年 zj. All rights reserved.
//

import UIKit

// 定义全局常量
private let cellId = "cellId"

class WBHomeViewController: WBBaseViewController {

//    private lazy var statusList = [String]()
    private lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func loadData() {
        //
        print("准备刷新，最后一条 \(self.listViewModel.statusList.last?.text)")
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            print("加载数据结束")
            self.refreshControl?.endRefreshing()
            self.isPullup = false
            
            if shouldRefresh {
                self.tableView?.reloadData()
            }
        }
    }
    
    @objc private func showFriends() {
//        print(#function)
        
        let vc = WBDemoViewController()
//        vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
    override func setupTableView() {
        super.setupTableView()
        
        //navigationItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", style: UIBarButtonItemStyle.plain, target: self, action: #selector(showFriends))
//        let btn = UIButton()
//        btn.setTitle("好友", for: UIControlState.normal)
//        btn.setTitle("好友", for: UIControlState.highlighted)
//        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
//        btn.setTitleColor(UIColor.darkGray, for: UIControlState.normal)
//        btn.setTitleColor(UIColor.orange, for: UIControlState.highlighted)
//        btn.addTarget(self, action: #selector(showFriends), for: UIControlEvents.touchUpInside)
//        btn.sizeToFit()
//
//        navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: btn)
        
        navItem.leftBarButtonItem = UIBarButtonItem.init(title: "好友", target: self, action: #selector(showFriends))
        
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        
        setupNavTitle()
    }
}

// MARK: - 表格数据源方法,具体的数据源实现
extension WBHomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        cell.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        return cell
    }
}

// MARK: - 设置界面
extension WBHomeViewController {
    // 设置导航栏标题
    private func setupNavTitle() {
        let title = WBNetworkManager.shared.userAccount.screen_name
        
        let button = WBTitleButton.init(title: title)
        
        navItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTileButton), for: .touchUpInside)
    }
    
    @objc private func clickTileButton(btn: UIButton) {
        btn.isSelected = !btn.isSelected
    }
}
